#!/bin/bash
set -e

vscodeRef=$(cat package.json | jq -r '.config.vscode.ref')
monacoRef=$(cat package.json | jq -r '.config.monaco.ref')

package_json="`pwd`/package.json"
output_directory="`pwd`/vscode"
loc_output_directory="`pwd`/vscode-loc"
extension_output_directory="`pwd`/vscode-default-extensions"
patch_directory="`pwd`/vscode-patches"
version_info=$output_directory/version.info

if [[ -e $version_info && $(cat $version_info) == $vscodeRef ]]; then
    echo "vscode version $vscodeRef is already installed. Aborting..."
    exit 0
else
    echo "vscode version $vscodeRef is not available. Installing..."
fi

rm -rf $output_directory

build_directory=`mktemp -d`
echo "Downloading vscode $vscodeRef in $build_directory..."

curl -L --max-redirs 5 https://github.com/Microsoft/vscode/tarball/$vscodeRef | tar -xz -C $build_directory --strip-components=1
vscodeVersion=$(cat "$build_directory/package.json" | jq -r '.["version"]')
vscodeCommit=$(curl "https://api.github.com/repos/Microsoft/vscode/git/refs/tags/$vscodeRef" | jq -r '.["object"]["sha"]')
cat <<< "$(jq ".config.vscode.version = \"$vscodeVersion\"" $package_json)" > $package_json
cat <<< "$(jq ".config.vscode.commit = \"$vscodeCommit\"" $package_json)" > $package_json

cd $build_directory

echo "Patching vscode..."
find "$patch_directory" -type f -name '*.patch' -print0 | sort -z | xargs -t -0 -n 1 patch -p1 -i

echo "Installing vscode dependencies..."
npm ci

echo "Installing build dependencies"
cd build
npm ci
cd ..

echo "Extracting service identifiers"
node build/lib/extractServices.js

echo "Applying transformConstEnums"
node build/lib/transformConstEnums.js

## Change shake level from ClassMembers to Files to speed up build
sed -i'' -e 's/shakeLevel: 2/shakeLevel: 0/g' build/gulpfile.editor.js
## build editor editor.api.d.ts
npx gulp editor-distro-resources

# build and copy default extensions
BUILD_SOURCEVERSION=$vscodeRef NODE_OPTIONS=--max-old-space-size=8192 npx gulp compile-web-extensions-build
rm -rf $extension_output_directory
cp -R .build/web/extensions "$extension_output_directory"

# Remove useless files
cd src
rm -rf `find . -name '*.test.ts' -o -name 'test' -o -name 'electron-browser'`
rm -f bootstrap*.ts *main.ts *cli.ts
rm -rf vs/code

# generate vscode.proposed.d.ts
cd vscode-dts
echo "" > vscode.proposed.d.ts

for file in vscode.proposed.*.d.ts; do
	echo "import \"./$file\"" >> vscode.proposed.d.ts
done
cd ..

mkdir -p $output_directory

echo "Building vscode..."
NODE_OPTIONS=--max-old-space-size=8192 npx tsc --declaration --importHelpers --outDir "$output_directory/src" --skipLibCheck --rootDir .
# Copy files that are already built and assets
find ./ \( -name '*.js' -o -name '*.d.ts' -o -name '*.ttf' -o -name '*.css' -o -name '*.mp3' -o -name '*.scm' -o -name '*.svg' -o -name '*.png' -o -name '*.html' -o -name '*.sh' -o -name '*.zsh' -o -name '*.ps1' \) -exec rsync -R \{\} "$output_directory/src" \;

cd ..
cp package.json product.json $output_directory
# Copy editor types
cp out-monaco-editor-core/esm/vs/editor/editor.api.d.ts $output_directory/src/vs/editor/editor.api.d.ts

echo "Downloading vscode-loc"
rm -rf $loc_output_directory
mkdir -p $loc_output_directory
curl -L --max-redirs 5 https://github.com/microsoft/vscode-loc/archive/refs/heads/main.tar.gz | tar -xz -C $loc_output_directory --strip-components=2 vscode-loc-main/i18n

echo $vscodeRef > $version_info

rm -rf $build_directory
